<?php
namespace App\Services\AdminService;
use Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Lang;
use Illuminate\Http\Request;
use App\Services\Tool\GeneralTool;
class BackstageAdminService
{
    /**
     * 添加验证
     * @param array $admin
     * @return mixed
     */
    public function loginValidator(array $admin)
    {
        $v = [
            'username' => 'required|max:50',
            'password' => 'required|max:50',
        ];
        return  Validator::make(
            $admin,$v

        );
    }

    /**
     * 通过用户名密码获取用户
     * @param $username
     * @param $password
     */
    public function getAdminForUsername($username)
    {
		
        $user = DB::table('admins')
				->where(['username'=>$username,'state'=>1])
				->select('id', 'username', 'email', 'updated_at')
				->first();
		return $user;
    }
    public function getAdminById($admin_id)
    {
        return DB::table('admins')
            ->where('state',1)
            ->where('id',$admin_id)
            ->select('id', 'username', 'email', 'updated_at')
            ->first();
    }
    /***
     * 删除管理员
     * @param $id
     * @return bool
     */
    public function delAdminById($id)
    {
        if(empty($id)){
            return false;
        }
        $res=DB::table('admins')
            ->where('id',$id)
            ->update(array(
                'state'=>0,
                'updated_at'=>time()
            ));
        return ($res==1)?true:false;
    }
}